package com.yqb.study.common.pagination;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.plugins.Page;
import com.yqb.study.constant.pagination.OrderEnum;
import com.yqb.study.support.HttpKit;
import com.yqb.study.util.ToolUtil;

/**
 * BootStrap Table默认的分页参数创建
 * 如果有分页查询请求，模拟BootStrap Table查询的参数
 */
public class PageFactory<T> {

	public Page<T> defaultPage() {
		HttpServletRequest request = HttpKit.getRequest();
		// 默认一页显示14条
		int limit = 14;
		if (!StringUtils.isBlank(request.getParameter("limit"))) {
			limit = Integer.valueOf(request.getParameter("limit"));
		}
		// 默认从第一条开始查询
		int offset = 0;
		if (!StringUtils.isBlank(request.getParameter("offset"))) {
			offset = Integer.valueOf(request.getParameter("offset"));
		}
		// 查看是否有排序要求
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		if (ToolUtil.isEmpty(sort)) {
			Page<T> page = new Page<>((offset / limit + 1), limit);
			page.setOpenSort(false);
			return page;
		} else {
			Page<T> page = new Page<>((offset / limit + 1), limit, sort);
			if (OrderEnum.ASC.getDes().equals(order)) {
				page.setAsc(true);
			} else {
				page.setAsc(false);
			}
			return page;
		}
	}
}
